package com.acce.service.utils;

import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.commons.lang.StringUtils;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import com.alibaba.fastjson.JSONObject;

public class XmlUtils {
	
//	private static String xml = "<?xml  version=\"1.0\"encoding=\"gbk\" ?><BEDC><Message><commHead><tranCode>0001</tranCode><cifMaster>10006694</cifMaster><entSeqNo>2013082000010002</entSeqNo><tranDate>20130820</tranDate><tranTime>110341</tranTime><retCode>407</retCode></commHead><Body/></Message></BEDC>";

	private static String xml = "<?xml version='1.0' encoding='utf-8' standalone='yes' ?><context><head><service_code>9003</service_code><sign>81ce32d712843dff3e6d38385dafc54c7a7ac364</sign><timestamp>20160610175300</timestamp><nonce>658475321</nonce></head><body><farmerMerchants></farmerMerchants><uid>51903a88-5b4d-4d3f-96fe-19dd3a3677e3</uid></body></context>";
	public static Document readDocument(String xml) throws DocumentException{
//		SAXReader reader = new SAXReader();
		Document document = DocumentHelper.parseText(xml);
		return document;
	}
	
	public static Element getRoot(Document doc) {
		Element root = doc.getRootElement();
		return root;
	}
	
	public static Element getNodeInHead(Element root, String name){
		Element node = root.element("head").element(name);
		return node;
	}
	
	public static JSONObject xmlToJsonByHead(String xml, JSONObject json) throws DocumentException{
		Element root = getRoot(readDocument(xml));
		Element head = root.element("head");
		Element serviceCode = head.element("targetCode");
		Element returnCode = head.element("returnCode");
		Element returnMsg = head.element("returnMsg");
		Element cacheTime = head.element("cacheTime");
		Element checkTime = head.element("checkTime");
		json.put("serviceCode", serviceCode.getText());
		json.put("returnCode", returnCode.getText());
		json.put("returnMsg", returnMsg.getText());
		json.put("cacheTime", cacheTime.getText());
		json.put("checkTime", checkTime.getText());
		return json;
	}
	
	public static JSONObject xmlToJsonByBody(String xml, JSONObject json) throws DocumentException{
		Element root = getRoot(readDocument(xml));
		Element head = root.element("body");
		String serviceCode = head.element("targetCode").getText();
		if(!StringUtils.isEmpty(serviceCode)){
			serviceCode = serviceCode.replace("U", "A");
		}
		if("5003A01".equals(serviceCode)){
			Element result = head.element("result");
			json.put("result", result.getText());
		}
		return json;
	}
	
	public static String createNsyXml(Map<String, String> headMap, Map<String, String> bodyMap){
		String xml = null;
		Element root = DocumentHelper.createElement("content");
		Document document = DocumentHelper.createDocument(root);
		Element head = root.addElement("head");
		Iterator<Entry<String, String>> ith = headMap.entrySet().iterator();
		while(ith.hasNext()){
			Map.Entry<String, String> entry = ith.next();
			Element ele = head.addElement(entry.getKey());
			ele.setText(entry.getValue());
		}
		Element body = root.addElement("body");
		Iterator<Entry<String, String>> itb = bodyMap.entrySet().iterator();
		while(itb.hasNext()){
			Map.Entry<String, String> entry = itb.next();
			Element ele = body.addElement(entry.getKey());
			ele.setText(entry.getValue());
		}
		xml = document.asXML();
		System.out.println(xml);
		return xml;
	}
	
	public static void main(String[] args) throws Exception{
		Map map = new LinkedHashMap();
		map.put("1", "a");
		map.put("2", "b");
		map.put("3", "c");
		createNsyXml(map, map);
		
//		Document doc = XmlUtils.readDocument(XmlUtils.xml);
//		Element root = doc.getRootElement();
//		System.out.println(root.getName() + ":" + root.getText());
//		
//		Element message = root.element("head");
//		System.out.println(message.getName() + ":" + message.getText());
//		
//		Element body = root.element("body");
//		System.out.println(body.getName() + ":" + body.getText());
//		
//		Element commHead = message.element("commHead");
//		System.out.println(commHead.getName() + ":" + commHead.getText());
//		
//		Element tranCode = commHead.element("tranCode");
//		System.out.println(tranCode.getName() + ":" + tranCode.getText());
		
//		Element node1 = getElementByName(root, "retCode");
//		System.out.println(node1.getName() + ":" + node1.getText());
	}
}
